// ActionScript file
	
	private var bTypeForGuideView:String;
	private var tTypeForGuideView:String;
	[Bindable] private var reportComment:String;

	private function start_basic():void{		
		testType = "basic";
		testNum = 1;
		area_test.text = que_basic[0];
		eS=0;eN=0;iS=0;iN=0;eT=0;eF=0;iT=0;iF=0;
	}
	
	private function start_simple():void{		
		testType = "simple";
		testNum = 1;
		area_test.text = que_simple[0];
		eS=0;eN=0;iS=0;iN=0;eT=0;eF=0;iT=0;iF=0;
	}
	
	
	private function nextQuestion():void{
		if(testType == "basic"){
			if(testNum != 8){
				switch(testNum){
					case 1:
						if(hsd_choose.value <= 0){eS += (hsd_choose.value * -1);}else{eN += (hsd_choose.value);}
						break;
					case 2:
						if(hsd_choose.value <= 0){iS += (hsd_choose.value * -1);}else{iN += (hsd_choose.value);}
						break;
					case 3:
						if(hsd_choose.value <= 0){eS += (hsd_choose.value * -1);}else{iS += (hsd_choose.value);}
						break;
					case 4:
						if(hsd_choose.value <= 0){eN += (hsd_choose.value * -1);}else{iN += (hsd_choose.value);}
						break;
					case 5:
						if(hsd_choose.value <= 0){eT += (hsd_choose.value * -1);}else{eF += (hsd_choose.value);}
						break;
					case 6:
						if(hsd_choose.value <= 0){iT += (hsd_choose.value * -1);}else{iF += (hsd_choose.value);}
						break;
					case 7:
						if(hsd_choose.value <= 0){eT += (hsd_choose.value * -1);}else{iT += (hsd_choose.value);}
						break;
				}
				area_test.text = que_basic[testNum]; //new question load
				testNum++;
			}else if(testNum == 8)
			{
				if(hsd_choose.value <= 0){eF += (hsd_choose.value * -1);}else{iF += (hsd_choose.value);}
				viewReport();
				area_test.text = "";
			}
		}else if(testType == "simple"){
			if(testNum != 24){
				if(testNum >= 1 && testNum < 4){
					if(hsd_choose.value <= 0){eS += (hsd_choose.value * -1);}else{eN += (hsd_choose.value);}}
				else if(testNum >= 4 && testNum < 7){
					if(hsd_choose.value <= 0){iS += (hsd_choose.value * -1);}else{iN += (hsd_choose.value);}}
				else if(testNum >= 7 && testNum < 10){
					if(hsd_choose.value <= 0){eS += (hsd_choose.value * -1);}else{iS += (hsd_choose.value);}}
				else if(testNum >= 10 && testNum < 13){
					if(hsd_choose.value <= 0){eN += (hsd_choose.value * -1);}else{iN += (hsd_choose.value);}}
				else if(testNum >= 13 && testNum < 16){
					if(hsd_choose.value <= 0){eT += (hsd_choose.value * -1);}else{eF += (hsd_choose.value);}}
				else if(testNum >= 16 && testNum < 19){
					if(hsd_choose.value <= 0){iT += (hsd_choose.value * -1);}else{iF += (hsd_choose.value);}}
				else if(testNum >= 19 && testNum < 22){
					if(hsd_choose.value <= 0){eT += (hsd_choose.value * -1);}else{iT += (hsd_choose.value);}}
				else if(testNum >= 22 && testNum < 24){
					if(hsd_choose.value <= 0){eF += (hsd_choose.value * -1);}else{iF += (hsd_choose.value);}
				}
				area_test.text = que_simple[testNum]; //new question load
				testNum++;
			}else if(testNum == 24)
			{
				if(hsd_choose.value <= 0){eF += (hsd_choose.value * -1);}else{iF += (hsd_choose.value);}
				viewReport();
				area_test.text = "";
			}
		}
		hsd_choose.value=0; // Hslider 중앙값 설정
	}
	
	private function analizeBasicType(eS:int,eN:int,eT:int,eF:int,iS:int,iN:int,iT:int,iF:int):String{
		var type:String;
		var G1:int=eS+eT+iN+iF;	var G2:int=eS+eF+iN+iT;	var G3:int=eN+eT+iS+iF;	var G4:int=eN+eF+iS+iT;
		if(G1>=G2 && G1>=G3 && G1>=G4){
			if(eS+iF >= iN+eT){type = "SFM";}else if(eS+iF < iN+eT){type = "NTV";}
		}else if(G2>=G1 && G2>=G3 && G2>=G4){
			if(eS+iT >= iN+eF){type = "STM";}else if(eS+iT < iN+eF){type = "NFV";}
		}else if(G3>=G1 && G3>=G2 && G3>=G4){
			if(eN+iF >= iS+eT){type = "NFM";}else if(eN+iF < iS+eT){type = "STV";}
		}else if(G4>=G1 && G4>=G2 && G4>=G3){
			if(eN+iT >= iS+eF){type = "NTM";}else if(eN+iT < iS+eF){type = "SFV";}
		}
		
		if(eS+eN+eT+eF+iS+iN+iT+iF == 0){type = "";}
		
		return type;
	}
	
	private function analizeTendenciatedType(eS:int,eN:int,eT:int,eF:int,iS:int,iN:int,iT:int,iF:int):String{
		var type:String;
		var S:int=eS+iS; var N:int=eN+iN; var T:int=eT+iT; var F:int=eF+iF; var P:int=S+N; var J:int=T+F;
		if(P>=J){
			if(S>=N){type=(T>=F)?"STP":"SFP";}else{type=(T>=F)?"NTP":"NFP";}
		}else{
			if(S>=N){type=(T>=F)?"STJ":"SFJ";}else{type=(T>=F)?"NTJ":"NFJ";}
		}
		
		if(eS+eN+eT+eF+iS+iN+iT+iF == 0){type = "";}
		return type;
	}
	
	private function analizeReliability(eS:int,eN:int,eT:int,eF:int,iS:int,iN:int,iT:int,iF:int):String{
		var reliability:Number = new Number();  // 0~1
		var G1:int=eS+eT+iN+iF;	var G2:int=eS+eF+iN+iT;	var G3:int=eN+eT+iS+iF;	var G4:int=eN+eF+iS+iT;
		
		if(G1>=G2 && G1>=G3 && G1>=G4){
			reliability = (G1!=0) ? (G1-G4)/G1 : 0;
		}else if(G2>=G1 && G2>=G3 && G2>=G4){
			reliability = (G2!=0) ? (G2-G3)/G2 : 0;
		}else if(G3>=G1 && G3>=G2 && G3>=G4){
			reliability = (G3!=0) ? (G4-G2)/G4 : 0;
		}else if(G4>=G1 && G4>=G2 && G4>=G3){
			reliability = (G4!=0) ? (G4-G1)/G4 : 0;
		}
		
		if(eS+eN+eT+eF+iS+iN+iT+iF == 0){reliability = 0;}
		var temp:String = (reliability!=0) ? (reliability*100).toString().substr(0,5) : "0"
		return temp;
	}
	
	private function viewReport():void{
		reqServerTime.send();
		lab_today.text = "검사일자 : "+dateNow.substring(0,4)+"."+dateNow.substring(4,6)+"."+dateNow.substring(6);
		if(testType == "basic"){
			lab_testNum.text = "검사번호 : "+sid+"-"+"B"+dateNow+timeNow;
		}else if(testType == "simple"){
			lab_testNum.text = "검사번호 : "+sid+"-"+"S"+dateNow+timeNow;
		}
		
		var bType:String = analizeBasicType(eS,eN,eT,eF,iS,iN,iT,iF);
		var tType:String = analizeTendenciatedType(eS,eN,eT,eF,iS,iN,iT,iF);
		var reliability:String = analizeReliability(eS,eN,eT,eF,iS,iN,iT,iF);
		var maxPoint:int;
		var report:String="";
		
		if(testType == "basic"){
			maxPoint = 40;
		}else if(testType == "simple"){
			maxPoint = 120;
		}
		
		if(testType == "basic"){report += "검사종류 : 기본검사\r";	}else if(testType == "simple"){report += "검사종류 : 단순문항검사\r";}
		
		var eC:int = 0; var iC:int = 0; var CtStr:String = "";
		CtStr = analizeFocusTendenciation(eS+eN+eT+eF, iS+iN+iT+iF);
		
		report += "기본유형 : "+bType+"\r";
		report += "경향화유형 : "+tType+"\r";
		report += "경향화방향 : "+CtStr+"\r";
		report += "검사정확도 : "+reliability+"%"+" (정확도가 높을 수록, 검사결과로 나온 기본유형이 당신의 선천적인 유형이라고 믿어도 좋습니다.)\r";
		report += "유형편향도 : "+((eS+eN+eT+eF+iS+iN+iT+iF)/maxPoint*100).toString().substr(0,5)+"%"+" (편향도가 높을 수록, 현재 당신의 경향화 상태가 강하다는 뜻입니다.)\r";
		report += "지표별 점수 : "+"eS:"+eS+", "+"eN:"+eN+", "+"eT:"+eT+", "+"eF:"+eF+", "+"iS:"+iS+", "+"iN:"+iN+", "+"iT:"+iT+", "+"iF:"+iF+"\r";
		tnav_test.selectedChild = can_report;
		area_report.text = report;
		
		if( (eS+eN+eT+eF+iS+iN+iT+iF) != 0 ){
			bTypeForGuideView = bType;
			tTypeForGuideView = tType;
			lab_refBtypeInGuide.text = "결과 : "+bType+" (기본유형)";
			lab_refTtypeInGuide.text = "결과 : "+tType+" (경향화유형)";
		}else{
			bType = tType = "";
			bTypeForGuideView = tTypeForGuideView = "";
			lab_refBtypeInGuide.text = "결과 : 검사를 올바르게 수행하지 않으셨습니다.";
			lab_refTtypeInGuide.text = "결과 : 검사를 올바르게 수행하지 않으셨습니다.";
		}
	}
	
	private function analizeFocusTendenciation(eC:int, iC:int):String{
		var Ct:Number = (eC-iC)/(eC+iC);
		var CtStr:String = "";
		
		if(eC!=0 || iC!=0){
			if(Ct > 0){			CtStr = "외적경향화, "+(Ct*100).toString().substr(0,5)+"%";}
			else if(Ct < 0){	CtStr = "내적경향화, "+(-Ct*100).toString().substr(0,5)+"%";	}
			else{				CtStr = "경향화 결정 불능";}
		}else{
			CtStr = "경향화 결정 불능";
		}
		
		return CtStr;
	}
	
	private function refBtypeInGuide():void{
		if(Boolean(bTypeForGuideView)){
			tnav_main.selectedChild = can_guide;
			cbx_MainGuide.selectedItem = "기본유형 설명";
			cbx_SubGuide.dataProvider = BtypeinfoGuideArr;
			
			switch(bTypeForGuideView){
				case "SFM": reqGetGuideText.url = "text/G1.txt";	cbx_SubGuide.selectedItem = "1군:(SFM,NTV)";		break;
				case "NTV": reqGetGuideText.url = "text/G1.txt";	cbx_SubGuide.selectedItem = "1군:(SFM,NTV)";		break;
				
				case "STM": reqGetGuideText.url = "text/G2.txt";	cbx_SubGuide.selectedItem = "2군:(STM,NFV)";		break;
				case "NFV": reqGetGuideText.url = "text/G2.txt";	cbx_SubGuide.selectedItem = "2군:(STM,NFV";		break;
				
				case "NFM": reqGetGuideText.url = "text/G3.txt";	cbx_SubGuide.selectedItem = "3군:(NFM,STV)";		break;
				case "STV": reqGetGuideText.url = "text/G3.txt";	cbx_SubGuide.selectedItem = "3군:(NFM,STV)";		break;
				
				case "NTM": reqGetGuideText.url = "text/G4.txt";	cbx_SubGuide.selectedItem = "4군:(NTM,SFV)";		break;
				case "SFV": reqGetGuideText.url = "text/G4.txt";	cbx_SubGuide.selectedItem = "4군:(NTM,SFV)";		break;
			}
			reqGetGuideText.send();
		}
	}
	
	private function refTtypeInGuide():void{
		if(Boolean(tTypeForGuideView)){
			tnav_main.selectedChild = can_guide;
			cbx_MainGuide.selectedItem = "경향화유형 설명";
			cbx_SubGuide.dataProvider = TtypeinfoGuideArr;
			
			switch(tTypeForGuideView){
				case "STJ": reqGetGuideText.url = "text/TendenciatingToT.txt";	cbx_SubGuide.selectedItem = "사고화(STJ,NTJ)";		break;
				case "NTJ": reqGetGuideText.url = "text/TendenciatingToT.txt";	cbx_SubGuide.selectedItem = "사고화(STJ,NTJ)";		break;
				case "SFJ": reqGetGuideText.url = "text/TendenciatingToF.txt";	cbx_SubGuide.selectedItem = "감정화(SFJ,NFJ)";		break;
				case "NFJ": reqGetGuideText.url = "text/TendenciatingToF.txt";	cbx_SubGuide.selectedItem = "감정화(SFJ,NFJ)";		break;
				case "STP": reqGetGuideText.url = "text/TendenciatingToS.txt";	cbx_SubGuide.selectedItem = "감각화(STP,SFP)";		break;
				case "SFP": reqGetGuideText.url = "text/TendenciatingToS.txt";	cbx_SubGuide.selectedItem = "감각화(STP,SFP)";		break;
				case "NTP": reqGetGuideText.url = "text/TendenciatingToN.txt";	cbx_SubGuide.selectedItem = "직관화(NTP,NFP)";		break;
				case "NFP": reqGetGuideText.url = "text/TendenciatingToN.txt";	cbx_SubGuide.selectedItem = "직관화(NTP,NFP)";		break;
			}
			reqGetGuideText.send();
		}
	}
	
	private function saveReport():void{
		if(testType!=""){
			// parsing point
			// combinedPoint = "eSeNeTeFiSiNiTiF" = 16자리 String.
			combinedPoint = ""
			combinedPoint += (eS.toString().length==1) ? ("0"+eS.toString()) : eS.toString();
			combinedPoint += (eN.toString().length==1) ? ("0"+eN.toString()) : eN.toString();
			combinedPoint += (eT.toString().length==1) ? ("0"+eT.toString()) : eT.toString();
			combinedPoint += (eF.toString().length==1) ? ("0"+eF.toString()) : eF.toString();
			combinedPoint += (iS.toString().length==1) ? ("0"+iS.toString()) : iS.toString();
			combinedPoint += (iN.toString().length==1) ? ("0"+iN.toString()) : iN.toString();
			combinedPoint += (iT.toString().length==1) ? ("0"+iT.toString()) : iT.toString();
			combinedPoint += (iF.toString().length==1) ? ("0"+iF.toString()) : iF.toString();
			
			reportComment = escape(tput_reportComment.text);
			
			// send Report
			reqSaveReport.send();
			testType="";
		}else{
			Alert.show("아직 Test를 수행하지 않으셨어요 ^^;");
		}
	}
	
	private function savedReport():void{
		// move Focus
		tnav_main.selectedChild = can_profile;
		tnav_profile.selectedChild = can_personality;
		dgrid_report.selectedIndex = dataset_report.length-1;
		
		// reload Reports 
		reqGetReport.send();
		reviewReport();
		deleteReport();
	}
	
	private function deleteReport():void{
		testNum = 0;
		testType = "";
		area_test.text = "";
		report = "";
		area_report.text = "";
		tput_reportComment.text = "";
	}
	
	private function getServerTime(event:ResultEvent):void{
		dataset_today.removeAll();
		dataset_today = new XMLListCollection(new XMLList(event.result));
		dateNow = dataset_today.child("date").text().toString();
		timeNow = dataset_today.child("time").text().toString();
	}
	
	private function goBasetheoryInfo():void{
		tnav_main.selectedChild = can_guide;
		cbx_SubGuide.dataProvider = HelpGuideArr;
		reqGetGuideText.url = "text/BasetheoryInfo.txt";
		reqGetGuideText.send();
		
		cbx_MainGuide.selectedItem = "앱 도움말"
		cbx_SubGuide.dataProvider = HelpGuideArr;
		cbx_SubGuide.selectedItem = "BaseTheory 성격유형 검사"
	}